package com.wing.cabinet.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.StorageInventoryInout;
import com.wing.cabinet.model.entity.UseInventoryInout;
import com.wing.cabinet.model.request.UseInventoryInoutForm;
import com.wing.common.constant.CabAuthCode;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.UseInventoryInoutDetail;
import com.wing.cabinet.model.response.UseInventoryInoutDetailVO;
import com.wing.cabinet.service.UseInventoryInoutDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.UseInventoryInoutDetailForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-售刀柜领取/暂存柜上/暂存/维修/回收明细管理", tags = "售刀柜领取/暂存柜上/暂存/维修/回收明细管理")
@RestController("appUseInventoryInoutDetailController")
@RequestMapping("cabinet/app/useInventoryInoutDetail")
@Slf4j
public class UseInventoryInoutDetailController {

    @Autowired
    private UseInventoryInoutDetailService useInventoryInoutDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<UseInventoryInoutDetailVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<UseInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<UseInventoryInoutDetailVO> list = useInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long useInventoryInoutDetailId) {
        UseInventoryInoutDetailVO useInventoryInoutDetailVO = useInventoryInoutDetailService.get4appById(useInventoryInoutDetailId);
        if (useInventoryInoutDetailVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), useInventoryInoutDetailVO);
    }

    @PostMapping()
    public JsonResult add(@Validated UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        if (useInventoryInoutDetailService.add(useInventoryInoutDetailForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }
    /**
    @ApiOperation(value = "存暂存刀具", notes = "")
    @PostMapping("/inventoryOut/storage")
    public JsonResult inventoryStorageIn(List<UseInventoryInoutDetailForm> list){
        try{
            useInventoryInoutDetailService.inventoryOut(list,null);
            return JsonResult.success();
        }catch (Exception e){
            log.error("storage inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "存维修刀具", notes = "")
    @PostMapping("/inventoryOut/repair")
    public JsonResult inventoryRepairIn(List<UseInventoryInoutDetailForm> list){
        try{
            useInventoryInoutDetailService.inventoryOut(list,null);
            return JsonResult.success();
        }catch (Exception e){
            log.error("repair inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }
    @ApiOperation(value = "存回收/报废刀具", notes = "")
    @PostMapping("/inventoryOut/recycle")
    public JsonResult inventoryRecycleIn(List<UseInventoryInoutDetailForm> list){
        try{
            useInventoryInoutDetailService.inventoryOut(list,null);
            return JsonResult.success();
        }catch (Exception e){
            log.error("recycle inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }*/

    @ApiOperation(value = "报失刀具", notes = "")
    @PostMapping("/lose/inventoryOut")
    public JsonResult inventoryRecycleIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("storage recycle param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.LOSE_KNIFE_AUTH_CODE)){
                return JsonResult.fail("此用户无报失权限,请配置此用户角色的报失权限");
            }
            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
            useInventoryInoutForm.getDetailList().get(0).setRemark(useInventoryInoutForm.getRemark());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            useInventoryInoutDetailService.inventoryOut(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.loss, UseInventoryInout.Type.loss
                    ,StorageInventoryInout.BusinessDetailType.loss,useInventoryInoutForm.getRemark());
            return JsonResult.success();
        } catch (Exception e) {
            log.error("recycle inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

}
